function System() {

	var self = this;
	
	var sec;
	var tim;
	var bar;
	var width;
	var del;
	var zmena;
	var cas;
	var pocatecni;
	var sessionExpires = null;
	
	self.confirm = function(text) {

		if (confirm(text)) {
			return true;
		} else {
			return false;
		}

	}

	self.init = function(){
		
		if(typeof sess_exp != "undefined"){
			sessionExpires = sess_exp;
			self.nastavSec(sessionExpires);
		}

	}

	self.nastavSec = function(seconds){

		//document.getElementById("bar").style.width = "200px";

		sec = seconds;
		bar = document.getElementById("bar").style.width;
		width = parseInt(bar);
		del = width / sec;
		pocatecni = self.preved(seconds);
		self.timer();

	}

	self.preved = function(sec){
		var t = sec / 60;
		if (t > 1) {
			sec = sec - (Math.ceil(t) - 1) * 60;
			if (sec == 60)
				sec = 0;
			if (sec == 0) {
				var s = "";
			} else {
				var s = sec + "s";
			}
			cas = Math.floor(t) + "m " + s;
		} else {
			cas = sec + "s";
		}
		return cas;
	}

	self.timer = function(){
		var noPx = document.childNodes ? 'px' : 0;

		if (sec == 0) {
		} else {
			sec = sec - 1;
			zmena = parseFloat(bar) - del;
			if (Math.abs(zmena) < 0.5)
				zmena = 0;
			bar = (zmena) + noPx;

			cas = self.preved(sec);

			if (zmena == 0) {
				window.location.href = base_address + "login/logout";
			} else {
				document.getElementById("timer").innerHTML = cas + " / "
						+ pocatecni;
				document.getElementById("bar").style.width = bar;
			}
			tim = setTimeout(self.timer, 1000);
		}
	};
	
	self.spinner = function(){

		// zobrazí spinner, signalizující uživateli, že se něco děje
	    if(typeof $("#ajax-spinner") == "undefined"){
			$('<div id="ajax-spinner">Nahrávám data...</div>').css({
	
		
		    }).ajaxStop(function() {
		           $("#ajax-spinner").remove(); // po skončení spinner smaž
		
		    }).appendTo("#snippet--flash");
	    }
	};
	
	self.refreshSnippets = function(payload){
		/*alert(payload.snippets + "asd");
		for(var i=0;i<payload.snippets.length;i++){
			alert(payload.snippets[i]);
			//$("#snippet--" + payload[i]).html(payload[i]);
		}*/
	};
	
	self.initWysiwyg = function(){
		tinyMCE.init({
			//language: "cs",
			entity_encoding : "raw",
			mode : "textareas",
			editor_selector : "mceEditor",
			theme : "advanced",
			plugins:"spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,inlinepopups,insertdatetime,preview,media,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
			file_browser_callback : "tinyBrowser",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			extended_valid_elements : "iframe[src|width|height|name|align]"
			
			});
	};

}

var system = new System();


$(document).ready(function(){
	system.init();

});